package com.punai.standard.controller;

import cn.hutool.core.io.FileUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.enums.BusinessType;
import com.punai.common.properties.SqliteUpPathConfig;
import com.punai.common.utils.StringUtils;
import com.punai.standard.domain.StaVersion;
import com.punai.standard.domain.constants.StaConstants;
import com.punai.standard.service.IStaGxFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 工序分包费用项Controller
 * 
 * @author zxw
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/sta/gxFee")
public class StaGxFeeController extends BaseController
{
    @Autowired
    private IStaGxFeeService staGxFeeService;


    @PostMapping("/ceshi")
    public AjaxResult ceshi() {
        return AjaxResult.success("123");
    }


    @PreAuthorize("@ss.hasPermi('sta:gxFee:previewpdf')")
    @Log(title = StaConstants.GX_NAME + "-预览PDF", businessType = BusinessType.INSERT)
    @PostMapping("/steamGxpdf2")
    public  ResponseEntity<InputStreamResource> createpdf(@RequestBody StaVersion staVersion) throws FileNotFoundException {
        String versionId = staVersion.getId();
        if(StringUtils.isEmpty(versionId)){
            throw new RuntimeException("版本号获取失败");
        }
        String pdfPath = SqliteUpPathConfig.getTemplatezippath() + versionId + ".pdf";
        File file = new File(pdfPath);

        if (!file.exists()) {
            staGxFeeService.createPdf(versionId);
        }
        // 设置响应��
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "标准费用项.pdf");

        // 创建文件输入流
        FileInputStream fileInputStream = new FileInputStream(file);

        // 创建输入流资源对象
        InputStreamResource resource = new InputStreamResource(fileInputStream);

        // 返回文件流
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }

    @PreAuthorize("@ss.hasPermi('sta:gxFee:previewpdf')")
    @Log(title = StaConstants.GX_NAME + "-预览PDF", businessType = BusinessType.INSERT)
    @PostMapping("/steamGxpdf1")
    public  void steamGxpdf(@RequestBody StaVersion staVersion, HttpServletResponse response) throws IOException {
        String versionId = staVersion.getId();
        if(StringUtils.isEmpty(versionId)){
            throw new RuntimeException("版本号获取失败");
        }
        // 设置响应的内容类型为PDF
        response.setContentType("application/pdf");
        // 设置响应的头部信息，指定文件名
        response.setHeader("Content-Disposition", "attachment; filename=" + "标准费用项.pdf");

        String pdfPath = SqliteUpPathConfig.getTemplatezippath() + versionId + ".pdf";
        File file = new File(pdfPath);
        if (!file.exists()) {
            staGxFeeService.createPdf(versionId);
        }
        FileInputStream fis = new FileInputStream(file);
        // 将PDF文件的内容写入响应的输出流
        OutputStream os = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.flush();
        os.close();
        fis.close();
    }

    @PreAuthorize("@ss.hasPermi('sta:gxFee:downloadpdf')")
    @Log(title = StaConstants.GX_NAME + "-预览PDF", businessType = BusinessType.INSERT)
    @PostMapping("/downloadGxpdf")
    public  void downloadGxpdf(String versionId, HttpServletResponse response) throws FileNotFoundException {
        if(StringUtils.isEmpty(versionId)){
            throw new RuntimeException("版本号获取失败");
        }
        String pdfPath = SqliteUpPathConfig.getTemplatezippath() + versionId +".pdf";
        File file = new File(pdfPath);
        if (!file.exists()) {
            staGxFeeService.createPdf(versionId);
        }
        File fileNew = new File(pdfPath);
        downloadFile(FileUtil.getInputStream(fileNew),response);
    }
}
